home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Chart.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-12  |  11.0 KB  |  382 lines

  1. //
  2. // "$Id: Fl_Chart.cxx,v 1.5.2.1 1999/06/12 12:38:14 mike Exp $"
  3. //
  4. // Forms-compatible chart widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. #include <FL/math.h>
  27. #include <FL/Fl.H>
  28. #include <FL/Fl_Chart.H>
  29. #include <FL/fl_draw.H>
  30. #include <string.h>
  31. #include <stdlib.h>
  32.  
  33. #define ARCINC    (2.0*M_PI/360.0)
  34.  
  35. // this function is in fl_boxtype.C:
  36. void fl_rectbound(int x,int y,int w,int h, Fl_Color color);
  37.  
  38. /* Widget specific information */
  39.  
  40. static void draw_barchart(int x,int y,int w,int h,
  41.               int numb, FL_CHART_ENTRY entries[],
  42.               double min, double max, int autosize, int maxnumb,
  43.               Fl_Color textcolor)
  44. /* Draws a bar chart. x,y,w,h is the bounding box, entries the array of
  45.    numb entries and min and max the boundaries. */
  46. {
  47.   double incr;
  48.   int zeroh;
  49.   double lh = fl_height();
  50.   if (max == min) incr = h;
  51.   else incr = h/(max-min);
  52.   if ( (-min*incr) < lh) {
  53.       incr = (h - lh + min*incr)/(max-min);
  54.       zeroh = int(y+h-lh);
  55.   } else {
  56.       zeroh = int(y+h+min * incr + .5);
  57.   }
  58.   int bwidth = int(w/double(autosize?numb:maxnumb)+.5);
  59.   /* Draw base line */
  60.   fl_color(textcolor);
  61.   fl_line(x, zeroh, x+w, zeroh);
  62.   if (min == 0.0 && max == 0.0) return; /* Nothing else to draw */
  63.   int i;
  64.   /* Draw the bars */
  65.   for (i=0; i<numb; i++) {
  66.       int h = int(entries[i].val*incr+.5);
  67.       if (h < 0)
  68.     fl_rectbound(x+i*bwidth,zeroh,bwidth+1,-h+1, (Fl_Color)entries[i].col);
  69.       else if (h > 0)
  70.     fl_rectbound(x+i*bwidth,zeroh-h,bwidth+1,h+1,(Fl_Color)entries[i].col);
  71.   }
  72.   /* Draw the labels */
  73.   fl_color(textcolor);
  74.   for (i=0; i<numb; i++)
  75.       fl_draw(entries[i].str,
  76.           x+i*bwidth+bwidth/2,zeroh,0,0,
  77.           FL_ALIGN_TOP);
  78. }
  79.  
  80. static void draw_horbarchart(int x,int y,int w,int h,
  81.                  int numb, FL_CHART_ENTRY entries[],
  82.                  double min, double max, int autosize, int maxnumb,
  83.                  Fl_Color textcolor)
  84. /* Draws a horizontal bar chart. x,y,w,h is the bounding box, entries the
  85.    array of numb entries and min and max the boundaries. */
  86. {
  87.   int i;
  88.   double lw = 0.0;        /* Maximal label width */
  89.   /* Compute maximal label width */
  90.   for (i=0; i<numb; i++) {
  91.       double w1 = fl_width(entries[i].str);
  92.       if (w1 > lw) lw = w1;
  93.   }
  94.   if (lw > 0.0) lw += 4.0;
  95.   double incr;
  96.   int zeroh;
  97.   if (max == min) incr = w;
  98.   else incr = w/(max-min);
  99.   if ( (-min*incr) < lw) {
  100.       incr = (w - lw + min*incr)/(max-min);
  101.       zeroh = x+int(lw+.5);
  102.   } else {
  103.       zeroh = int(x-min * incr + .5);
  104.   }
  105.   int bwidth = int(h/double(autosize?numb:maxnumb)+.5);
  106.   /* Draw base line */
  107.   fl_color(textcolor);
  108.   fl_line(zeroh, y, zeroh, y+h);
  109.   if (min == 0.0 && max == 0.0) return; /* Nothing else to draw */
  110.   /* Draw the bars */
  111.   for (i=0; i<numb; i++) {
  112.       int w = int(entries[i].val*incr+.5);
  113.       if (w > 0)
  114.     fl_rectbound(zeroh,y+i*bwidth,w+1,bwidth+1, (Fl_Color)entries[i].col);
  115.       else if (w < 0)
  116.     fl_rectbound(zeroh+w,y+i*bwidth,-w+1,bwidth+1,(Fl_Color)entries[i].col);
  117.   }
  118.   /* Draw the labels */
  119.   for (i=0; i<numb; i++)
  120.       fl_draw(entries[i].str,
  121.           zeroh-2,y+i*bwidth+bwidth/2,0,0,
  122.           FL_ALIGN_RIGHT);
  123. }
  124.  
  125. static void draw_linechart(int type, int x,int y,int w,int h,
  126.                int numb, FL_CHART_ENTRY entries[],
  127.                double min, double max, int autosize, int maxnumb,
  128.                Fl_Color textcolor)
  129. /* Draws a line chart. x,y,w,h is the bounding box, entries the array of
  130.    numb entries and min and max the boundaries. */
  131. {
  132.   int i;
  133.   double lh = fl_height();
  134.   double incr;
  135.   if (max == min) incr = h-2.0*lh;
  136.   else incr = (h-2.0*lh)/ (max-min);
  137.   int zeroh = int(y+h-lh+min * incr + .5);
  138.   double bwidth = w/double(autosize?numb:maxnumb);
  139.   /* Draw the values */
  140.   for (i=0; i<numb; i++) {
  141.       int x0 = x + int((i-.5)*bwidth+.5);
  142.       int x1 = x + int((i+.5)*bwidth+.5);
  143.       int y0 = i ? zeroh - int(entries[i-1].val*incr+.5) : 0;
  144.       int y1 = zeroh - int(entries[i].val*incr+.5);
  145.       if (type == FL_SPIKE_CHART) {
  146.       fl_color((Fl_Color)entries[i].col);
  147.       fl_line(x1, zeroh, x1, y1);
  148.       } else if (type == FL_LINE_CHART && i != 0) {
  149.       fl_color((Fl_Color)entries[i-1].col);
  150.       fl_line(x0,y0,x1,y1);
  151.       } else if (type == FL_FILLED_CHART && i != 0) {
  152.       fl_color((Fl_Color)entries[i-1].col);
  153.       if ((entries[i-1].val>0.0)!=(entries[i].val>0.0)) {
  154.           double ttt = entries[i-1].val/(entries[i-1].val-entries[i].val);
  155.           int xt = x + int((i-.5+ttt)*bwidth+.5);
  156.           fl_polygon(x0,zeroh, x0,y0, xt,zeroh);
  157.           fl_polygon(xt,zeroh, x1,y1, x1,zeroh);
  158.       } else {
  159.           fl_polygon(x0,zeroh, x0,y0, x1,y1, x1,zeroh);
  160.       }
  161.       fl_color(textcolor);
  162.       fl_line(x0,y0,x1,y1);
  163.       }
  164.   }
  165.   /* Draw base line */
  166.   fl_color(textcolor);
  167.   fl_line(x,zeroh,x+w,zeroh);
  168.   /* Draw the labels */
  169.   for (i=0; i<numb; i++)
  170.       fl_draw(entries[i].str,
  171.           x+int((i+.5)*bwidth+.5), zeroh - int(entries[i].val*incr+.5),0,0,
  172.           entries[i].val>=0 ? FL_ALIGN_BOTTOM : FL_ALIGN_TOP);
  173. }
  174.  
  175. static void draw_piechart(int x,int y,int w,int h,
  176.               int numb, FL_CHART_ENTRY entries[], int special,
  177.               Fl_Color textcolor)
  178. /* Draws a pie chart. x,y,w,h is the bounding box, entries the array of
  179.    numb entries */
  180. {
  181.   int i;
  182.   double xc,yc,rad;    /* center and radius */
  183.   double tot;        /* sum of values */
  184.   double incr;        /* increment in angle */
  185.   double curang;        /* current angle we are drawing */
  186.   double txc,tyc;    /* temporary center */
  187.   double lh = fl_height();
  188.   /* compute center and radius */
  189.   xc = x+w/2.0; yc = y+h/2.0;
  190.   rad = h/2.0 - lh;
  191.   if (special) { yc += 0.1*rad; rad = 0.9*rad;}
  192.   /* compute sum of values */
  193.   tot = 0.0;
  194.   for (i=0; i<numb; i++)
  195.     if (entries[i].val > 0.0) tot += entries[i].val;
  196.   if (tot == 0.0) return;
  197.   incr = 360.0/tot;
  198.   /* Draw the pie */
  199.   curang = 0.0;
  200.   for (i=0; i<numb; i++)
  201.     if (entries[i].val > 0.0)
  202.     {
  203.       txc = xc; tyc = yc;
  204.       /* Correct for special pies */
  205.       if (special && i==0)
  206.       {
  207.         txc += 0.3*rad*cos(ARCINC*(curang+0.5*incr*entries[i].val));
  208.         tyc -= 0.3*rad*sin(ARCINC*(curang+0.5*incr*entries[i].val));
  209.       }
  210.       fl_color((Fl_Color)entries[i].col);
  211.       fl_begin_polygon(); fl_vertex(txc,tyc);
  212.       fl_arc(txc,tyc,rad,curang, curang+incr*entries[i].val);
  213.       fl_end_polygon();
  214.       fl_color(textcolor);
  215.       fl_begin_loop(); fl_vertex(txc,tyc);
  216.       fl_arc(txc,tyc,rad,curang, curang+incr*entries[i].val);
  217.       fl_end_loop();
  218.       curang += 0.5 * incr * entries[i].val;
  219.       /* draw the label */
  220.       double xl = txc + 1.1*rad*cos(ARCINC*curang);
  221.       fl_draw(entries[i].str,
  222.           int(xl+.5),
  223.           int(tyc - 1.1*rad*sin(ARCINC*curang)+.5),
  224.           0, 0,
  225.           xl<txc ? FL_ALIGN_RIGHT : FL_ALIGN_LEFT);
  226.       curang += 0.5 * incr * entries[i].val;
  227.     }
  228. }
  229.  
  230. void Fl_Chart::draw() {
  231.     int xx,yy,ww,hh;
  232.     int i;
  233.  
  234.     xx = x()+9;
  235.     yy = y()+9;
  236.     ww = w()-2*9;
  237.     hh = h()-2*9;
  238.  
  239.     if (min >= max) {
  240.     min = max = 0.0;
  241.     for (i=0; i<numb; i++) {
  242.         if (entries[i].val < min) min = entries[i].val;
  243.         if (entries[i].val > max) max = entries[i].val;
  244.     }
  245.     }
  246.  
  247.     draw_box();
  248.     fl_font(textfont(),textsize());
  249.  
  250.     switch (type()) {
  251.     case FL_BAR_CHART:
  252.     draw_barchart(xx,yy,ww,hh, numb, entries, min, max,
  253.             autosize(), maxnumb, textcolor());
  254.     break;
  255.     case FL_HORBAR_CHART:
  256.     draw_horbarchart(xx,yy,ww,hh, numb, entries, min, max,
  257.             autosize(), maxnumb, textcolor());
  258.     break;
  259.     case FL_PIE_CHART:
  260.     draw_piechart(xx,yy,ww,hh,numb,entries,0, textcolor());
  261.     break;
  262.     case FL_SPECIALPIE_CHART:
  263.     draw_piechart(xx,yy,ww,hh,numb,entries,1,textcolor());
  264.     break;
  265.     default:
  266.     draw_linechart(type(),xx,yy,ww,hh, numb, entries, min, max,
  267.             autosize(), maxnumb, textcolor());
  268.     break;
  269.     }
  270.     draw_label();
  271. }
  272.  
  273. /*------------------------------*/
  274.  
  275. #define FL_CHART_BOXTYPE    FL_BORDER_BOX
  276. #define FL_CHART_COL1        FL_COL1
  277. #define FL_CHART_LCOL        FL_LCOL
  278. #define FL_CHART_ALIGN        FL_ALIGN_BOTTOM
  279.  
  280. Fl_Chart::Fl_Chart(int x,int y,int w,int h,const char *l) :
  281. Fl_Widget(x,y,w,h,l) {
  282.   box(FL_BORDER_BOX);
  283.   align(FL_ALIGN_BOTTOM);
  284.   numb       = 0;
  285.   maxnumb    = 0;
  286.   sizenumb   = FL_CHART_MAX;
  287.   autosize_  = 1;
  288.   min = max  = 0;
  289.   textfont_  = FL_HELVETICA;
  290.   textsize_  = 10;
  291.   textcolor_ = FL_BLACK;
  292.   entries    = (FL_CHART_ENTRY *)calloc(sizeof(FL_CHART_ENTRY), FL_CHART_MAX + 1);
  293. }
  294.  
  295. void Fl_Chart::clear() {
  296.   numb = 0;
  297.   redraw();
  298. }
  299.  
  300. void Fl_Chart::add(double val, const char *str, uchar col) {
  301.   /* Allocate more entries if required */
  302.   if (numb >= sizenumb) {
  303.     sizenumb += FL_CHART_MAX;
  304.     entries = (FL_CHART_ENTRY *)realloc(entries, sizeof(FL_CHART_ENTRY) * (sizenumb + 1));
  305.   }
  306.   // Shift entries as needed
  307.   if (numb >= maxnumb && maxnumb > 0) {
  308.     memcpy(entries, entries + 1, sizeof(FL_CHART_ENTRY) * (numb - 1));
  309.     numb --;
  310.   }
  311.   entries[numb].val = float(val);
  312.   entries[numb].col = col;
  313.     if (str) {
  314.     strncpy(entries[numb].str,str,FL_CHART_LABEL_MAX+1);
  315.     entries[numb].str[FL_CHART_LABEL_MAX] = 0;
  316.     } else {
  317.     entries[numb].str[0] = 0;
  318.     }
  319.   numb++;
  320.   redraw();
  321. }
  322.  
  323. void Fl_Chart::insert(int index, double val, const char *str, uchar col) {
  324.   int i;
  325.   if (index < 1 || index > numb+1) return;
  326.   /* Allocate more entries if required */
  327.   if (numb >= sizenumb) {
  328.     sizenumb += FL_CHART_MAX;
  329.     entries = (FL_CHART_ENTRY *)realloc(entries, sizeof(FL_CHART_ENTRY) * (sizenumb + 1));
  330.   }
  331.   // Shift entries as needed
  332.   for (i=numb; i >= index; i--) entries[i] = entries[i-1];
  333.   if (numb < maxnumb || maxnumb == 0) numb++;
  334.   /* Fill in the new entry */
  335.   entries[index-1].val = float(val);
  336.   entries[index-1].col = col;
  337.   if (str) {
  338.       strncpy(entries[index-1].str,str,FL_CHART_LABEL_MAX+1);
  339.       entries[index-1].str[FL_CHART_LABEL_MAX] = 0;
  340.   } else {
  341.       entries[index-1].str[0] = 0;
  342.   }
  343.   redraw();
  344. }
  345.  
  346. void Fl_Chart::replace(int index,double val, const char *str, uchar col) {
  347.   if (index < 1 || index > numb) return;
  348.   entries[index-1].val = float(val);
  349.   entries[index-1].col = col;
  350.   if (str) {
  351.       strncpy(entries[index-1].str,str,FL_CHART_LABEL_MAX+1);
  352.       entries[index-1].str[FL_CHART_LABEL_MAX] = 0;
  353.   } else {
  354.       entries[index-1].str[0] = 0;
  355.   }
  356.   redraw();
  357. }
  358.  
  359. void Fl_Chart::bounds(double min, double max) {
  360.   this->min = min;
  361.   this->max = max;
  362.   redraw();
  363. }
  364.  
  365. void Fl_Chart::maxsize(int m) {
  366.   int i;
  367.   /* Fill in the new number */
  368.   if (m < 0) return;
  369.   maxnumb = m;
  370.   /* Shift entries if required */
  371.   if (numb > maxnumb) {
  372.       for (i = 0; i<maxnumb; i++)
  373.       entries[i] = entries[i+numb-maxnumb];
  374.       numb = maxnumb;
  375.       redraw();
  376.   }
  377. }
  378.  
  379. //
  380. // End of "$Id: Fl_Chart.cxx,v 1.5.2.1 1999/06/12 12:38:14 mike Exp $".
  381. //
  382.